package com.hans.myscanner.entity;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/15.
 */
public class ContactEntity implements Parcelable {
    private String name;
    private List<String> phoneNums;
    private List<String> emails;
    private String address;


    //多个号码跟邮箱
    public ContactEntity() {
        name = address="";
        phoneNums = new ArrayList<>();
        emails = new ArrayList<>();
    }

    public ContactEntity(String name, String phoneNum) {
        this();
        this.name = name;
        List<String> phoneNums=new ArrayList<>();
        phoneNums.add(phoneNum);
        this.phoneNums=phoneNums;
    }

    public ContactEntity(String name, List<String> phoneNums, List<String> emails, String address) {
        this();
        this.name = name;
        this.phoneNums = phoneNums;
        this.emails = emails;
        this.address = address;
    }

    protected ContactEntity(Parcel in) {
        name = in.readString();
        phoneNums = in.createStringArrayList();
        emails = in.createStringArrayList();
        address = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeStringList(phoneNums);
        dest.writeStringList(emails);
        dest.writeString(address);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<ContactEntity> CREATOR = new Creator<ContactEntity>() {
        @Override
        public ContactEntity createFromParcel(Parcel in) {
            return new ContactEntity(in);
        }

        @Override
        public ContactEntity[] newArray(int size) {
            return new ContactEntity[size];
        }
    };

    //vcard 格式存储
//    1:  BEGIN:VCARD
//    2:  VERSION:3.0
//            3:  N:Gump;Forrest
//    4:  FN:Forrest Gump
//    5:  ORG:Bubba Gump Shrimp Co.
//            6:  TITLE:Shrimp Man
//    7:  PHOTO;VALUE=URL;TYPE=GIF:http://www.site.com/dir_photos/my_photo.gif
//            8:  TEL;TYPE=WORK,VOICE:(111) 555-1212
//            9:  TEL;TYPE=HOME,VOICE:(404) 555-1212
//            10:  ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
//    11:  LABEL;TYPE=WORK:100 Waters Edge\nBaytown, LA 30314\nUnited States of America
//    12:  ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
//    13:  LABEL;TYPE=HOME:42 Plantation St.\nBaytown, LA 30314\nUnited States of America
//    14:  EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
//    15:  REV:20080424T195243Z
//    16:  END:VCARD
    public static String toVCardFormat(ContactEntity contactEntity)
    {
        StringBuffer sb=new StringBuffer();//缓存
        sb.append("BEGIN:VCARD\nVERSON:3.0\n");
        if (contactEntity.getName()!=null) sb.append("FN:"+contactEntity.getName()+"\n");
        if (contactEntity.getPhoneNums()!=null) sb.append("FN:"+contactEntity.getPhoneNums()+"\n");
        if (contactEntity.getEmails()!=null) sb.append("FN:"+contactEntity.getEmails()+"\n");
        if (contactEntity.getAddress()!=null) sb.append("FN:"+contactEntity.getAddress()+"\n");
        sb.append("END:VCARD");
        return sb.toString();
    }

    private static final String VCARD_BEGIN="BEGIN:VCARD";
    private static final String VCARD_NAME="FN:";
    private static final String VCARD_TEL="TEL:";
    private static final String VCARD_EMAIL="EMAIL:";
    private static final String VCARD_ADR="ADR:";
    private static final String VCARD_END="END:VCARD";

    private static ContactEntity parseFromVCard(String vcardStr){
        if(TextUtils.isEmpty(vcardStr)) return null;
        if(!vcardStr.startsWith(VCARD_BEGIN)) return null;
        String[] lines=vcardStr.split("\n");
        if(lines.length < 3) return null;
        ContactEntity contactEntity=new ContactEntity();
        for(int i=0;i<lines.length;i++){
            if(lines[i].endsWith(":")) continue;
            if (lines[i].startsWith(VCARD_NAME)) contactEntity.setName(lines[i].substring(VCARD_NAME.length()));
            if (lines[i].startsWith(VCARD_TEL)) contactEntity.getPhoneNums().add(lines[i].substring(VCARD_TEL.length()));
            if (lines[i].startsWith(VCARD_EMAIL)) contactEntity.getEmails().add(lines[i].substring(VCARD_EMAIL.length()));
            if (lines[i].startsWith(VCARD_ADR)) contactEntity.setAddress(lines[i].substring(VCARD_ADR.length()));
            if (lines[i].startsWith(VCARD_END)) break;
        }
        return contactEntity;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getPhoneNums() {
        return phoneNums;
    }

    public void setPhoneNums(List<String> phoneNums) {
        this.phoneNums = phoneNums;
    }

    public List<String> getEmails() {
        return emails;
    }

    public void setEmails(List<String> emails) {
        this.emails = emails;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }


}
